-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Character count in addition to word and line counts #125
Comments
Hello @lasseeee , have you managed a custom implementation of this ? |
Nope, I didn't look into it further. I settled with backend validation. |
Hi @lasseeee and @HectorLS I'm going to post the function-related code I wrote, maybe it could help or be used in the core code of easymde if you like. <template>
<div class="vue-easymde">
<textarea
class="vue-simplemde-textarea"
:name="name"
:value="value"
@input="handleInput($event.target.value)"
/>
<div class="counters">
**{{countChars}}**
</div>
</div>
</template> <script>
...
computed: {
...
countChars(){
let stars = (/(\n| |^|\w)(\*+[a-zA-Z0-9_.-]+\*+)/g)
stars = this.countMatches(this.value,stars,'*')
let dbltilde = (/(\n| |^|\w)(~~[a-zA-Z0-9_.-]+~~)/g)
dbltilde = this.countMatches(this.value,dbltilde,'~')
let headers = (/(\n|^)(#+ )/g)
headers = this.countMatches(this.value,headers,'#')
let br = (/(<br>+)/g)
br = this.countMatches(this.value,br,'<br>')*4
let qt = (/(\n)(> )/g)
qt = this.countMatches(this.value,qt,'> ')*2
let to_remove = 0 + stars + headers + dbltilde + br +qt
let txt_length = this.value.length
//console.log("qt to remove: "+to_remove)
//console.log("total amount of chars: "+txt_length)
let cnt_string = this.lng.chars+(txt_length - to_remove) + " - " +
this.lng.standardPage+(((txt_length - to_remove)/1800).toFixed(1))
return cnt_string
},
...
},
methods: {
...
//returns the number of items matching or 0
countMatches(val,mtch,symbol){
let occurences=0
let result = val.match(mtch)
//console.log(result)
if(!result){
return occurences
}
else {
let res = result.toString()
let found = res.indexOf(symbol);
while (found !== -1) {
occurences++;
found = res.indexOf(symbol, found + 1);
}
//console.log("how many "+symbol+": "+occurences)
return occurences
},
...
}
</script> <style scoped>
@import "~highlight.js/styles/atom-one-dark.css";
@import "~easymde/dist/easymde.min.css";*/
.counters{
text-align: center;
font-size: 0.9em;
color: cadetblue;
}
</style> |
Did anyone manage to integrate this directly into easy mardown editor? |
Guys, I created a PR here: |
It would be great to be able to show the character count, not only words and lines. Character counts are helpful to meet a fields min or max character validation.
The text was updated successfully, but these errors were encountered: